<?php
namespace app\api\controller;

use app\api\annotation\LoginRequired;
use app\api\basic\BaseController;
use app\TalenInfo\logic\TalenInfoLogic;
use app\talent\logic\talentLogic;
use support\Request;
class TalentController extends BaseController
{
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
    /**
     * 建立信息填写情况
     */
    public  function  talentstatus(Request $request){
        $param['user_id']=$this->userId;
        if($param['user_id'] ==''){
          return  $this->fail('缺少参数');
        }
         $talent = new talentLogic();
         $TalenInfo = new TalenInfoLogic();
         $talentData =$talent->where(['user_id'=>$param['user_id']])
             ->whereNull('delete_time')
             ->find();

        $count1 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talentData['talent_id'],'type'=>1])->count();
        $count2 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talentData['talent_id'],'type'=>2])->count();
        $count3 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talentData['talent_id'],'type'=>3])->count();

         $talentData = empty($talentData)?[]:$talentData->toArray();
         $resulte['basic_information'] =!empty($talentData['name'])?true:false;
         $resulte['educationackgrounds'] =$count1>0?true:false;
         $resulte['work_experiences'] =$count2>0?true:false;
         $resulte['self_introduction'] =$count3>0?true:false;
         return $this->success(['data'=>$resulte]);
    }

    /**
     * 基础信息填写
     */
    public function information(Request $request){
        $param = $request->more([
            ['name',''],
            ['gender',''],
            ['birth_date',''],
            ['domicile',''],
            ['current_residence',''],
            ['education_level',''],
            ['graduation_school',''],
            ['english_level',''],
            ['career_intention',''],
            ['contact_number',''],
            ['email',''],
            ['age',0],
        ]);
        $param['user_id']=$this->userId;
        if($param['user_id'] =='' || $param['name'] == '' || $param['gender'] == '' || $param['birth_date'] == '' || $param['domicile']=='' || $param['current_residence'] == '')
        {
            return $this->fail('请完善必填项');
        }
        $param['birth_date'] = date('Y-m-d', strtotime($param['birth_date']));
        $talent  =new talentLogic();
        $talentData =$talent->where(['user_id'=>$param['user_id']])
            ->whereNull('delete_time')
            ->find();
        if($talentData){

            $talent->where(['user_id'=>$param['user_id']])->update($param);
        }else{
            $talent->insert($param);
        }
            return $this->success('更新成功');
    }

    /**
     * 教育经历填写
     */
    public function educationackgrounds(Request $request){
        $param = $request->more([
            ['id',0],
            ['educationackgrounds',[]],
        ]);
        $param['user_id']=$this->userId;
        if($param['user_id'] =='' || $param['educationackgrounds'] == []){
            $this->fail('缺少参数');
        }

        $educationackgrounds  = json_encode($param['educationackgrounds']);
        //return $this->success($param);

        $talent  =new talentLogic();
        $talents = $talent->where(['user_id'=>$param['user_id']])->find();
        //print_r($talents);
        $TalenInfo = new TalenInfoLogic();
        $data =[
            'talent_id'=>$talents['talent_id'],
            'user_id'=>$param['user_id'],
            'create_time'=>time(),
            'content'=>$educationackgrounds,
            'type'=>1,
        ];
        if($param['id'] >0){
            $TalenInfo->where(['id'=>$param['id']])->update($data);
        }else{
            $TalenInfo->insert($data);
        }
        $TalenInfo = new TalenInfoLogic();
        $data1 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talents['talent_id'],'type'=>1])->select();
        $resulte = [];
        foreach ($data1 as $key => $value){
            $resulte[]=json_decode($value['content'],true);
        }
        $talent->where(['talent_id'=>$talents['talent_id']])->update(['educationackgrounds'=>json_encode($resulte)]);
        return $this->success("更新成功");
    }

    /**
     * 工作经历填写
     */

    public function workexperiences(Request $request){
        $param = $request->more([
            ['id',0],
            ['work_experiences',[]],
        ]);
        $param['user_id']=$this->userId;
        if($param['user_id'] =='' || $param['work_experiences'] == []){
            $this->fail('缺少参数');
        }
        $work_experiences  = json_encode($param['work_experiences']);
        $talent  =new talentLogic();
        $talents = $talent->where(['user_id'=>$param['user_id']])->find();
        $TalenInfo = new TalenInfoLogic();
        $data =[
            'talent_id'=>$talents['talent_id'],
            'user_id'=>$param['user_id'],
            'create_time'=>time(),
            'content'=>$work_experiences,
            'type'=>2,
        ];
        if($param['id'] >0){
            $TalenInfo->where(['id'=>$param['id']])->update($data);
        }else{
            $TalenInfo->insert($data);
        }
        $TalenInfo = new TalenInfoLogic();
        $data1 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talents['talent_id'],'type'=>2])->select();
        $resulte = [];
        foreach ($data1 as $key => $value){
            $resulte[]=json_decode($value['content'],true);
        }
        $talent->where(['talent_id'=>$talents['talent_id']])->update(['work_experiences'=>json_encode($resulte)]);
        return $this->success("更新成功");
    }

    /**
     * 自我介绍填写
     */
 
    public function selfintroduction(Request $request){
        $param = $request->more([
            ['id',0],
            ['self_introduction',''],
            ['attachment_path',[]],
        ]);
        //print_r($param);
        $param['user_id']=$this->userId;
        if($param['user_id'] =='' || $param['self_introduction'] == ''){
            $this->fail('缺少参数');
        }
        $talent  =new talentLogic();
        $talents = $talent->where(['user_id'=>$param['user_id']])->find();
        $TalenInfo = new TalenInfoLogic();
        $data =[
            'talent_id'=>$talents['talent_id'],
            'user_id'=>$param['user_id'],
            'create_time'=>time(),
            'content'=>$param['self_introduction'],
            'type'=>3,
        ];

        $attachment_path = json_decode($param['attachment_path'],true);
        //$attachment_path  = json_encode($param['attachment_path']);
       // print_r($attachment_path);
        $TalenInfo->where(['talent_id'=>$talents['talent_id'],'type'=>4])->delete();
      //  $TalenInfo->where(['talent_id'=>$talents['talent_id'],'type'=>3])->delete();
        foreach ($attachment_path as $k =>$v){
            $datas =[
                'talent_id'=>$talents['talent_id'],
                'user_id'=>$param['user_id'],
                'create_time'=>time(),
                'content'=>$v,
                'type'=>4,
            ];
                $TalenInfo->insert($datas);
        }

        if($param['id'] >0){
            $TalenInfo->where(['id'=>$param['id']])->update($data);
        }else{
            $TalenInfo->where(['talent_id'=>$talents['talent_id'],'type'=>3])->delete();
            $TalenInfo->insert($data);
        }

        $data1 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talents['talent_id'],'type'=>3])->find();
        $data2 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$talents['talent_id'],'type'=>4])->select();
        $resulte = [];
      //  $res = [];
        foreach ($data2 as $key => $value){
           // print_r($value['id']);
            $resulte[][]=$value['content'];
        }
      //  print_r($resulte);
        $talent->where(['talent_id'=>$talents['talent_id']])->update(['self_introduction'=>$data1['content'],'attachment_path'=>json_encode($resulte)]);
        return $this->success("更新成功");
    }


    /**
     * 获取个人简历信息
     */
   
    public  function  talendList(Request $request){
        $param = $request->more([
            ['type',1],
        ]);
        $param['user_id']=$this->userId;
        //print_r($param);
        $takent =new talentLogic();
        $data =$takent->where('user_id',$param['user_id'])->find();
        $data = empty($data)?[]:$data->toArray();
        $TalenInfo = new TalenInfoLogic();
        $dataInfo = $TalenInfo->where(['type'=>$param['type'],'user_id'=>$param['user_id'],'talent_id'=>$data['talent_id']])->select();

       // print_r($dataInfo);
        $resutes = [];

            foreach ($dataInfo as $key=>&$value){
                if($param['type'] != 3){
                $value['content'] =json_decode($value['content'] ,true);
                }else{
                    $file = $TalenInfo->where(['type'=>4,'user_id'=>$param['user_id'],'talent_id'=>$data['talent_id']])->select();
                    $pic =[];
                    foreach ($file as $k =>$vls){

                       $pic[] = $vls['content'];
                    }
                    $dataInfo[$key]['pic'] = $pic;
                }
            }


        return $this->success(['data'=>$dataInfo]);
    }

    /**
     * 删除简历信息
     */
   
    public  function talentDelete(Request $request){
        $param = $request->more([
            ['type',1],
            ['id',0],
        ]);
        if($param['id'] == 0){
            return $this->fail('缺少参数');
        }
        $param['user_id']=$this->userId;
        // print_r($param);
        $takent =new talentLogic();
        $data =$takent->where('user_id',$param['user_id'])->find();
        $data = empty($data)?[]:$data->toArray();
        $TalenInfo = new TalenInfoLogic();
        $dataInfo = $TalenInfo->where(['type'=>$param['type'],'user_id'=>$param['user_id'],'talent_id'=>$data['talent_id']])->delete();
        $resulte = [];
        if($param['type'] ==1 || $param['type'] == 2){
            $data1 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$data['talent_id'],'type'=>$param['type']])->select();

            foreach ($data1 as $key => $value){
                $resulte[]=json_decode($value['content'],true);
            }
            if($param['type'] == 1 ) {
                $update ['educationackgrounds'] =json_encode($resulte);
            }
            if($param['type'] == 2 ) {
                $update ['educationackgrounds'] =json_encode($resulte);
            }
        }

        if($param['type'] == 3){
            $datas =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$data['talent_id'],'type'=>3])->find();
            $data2 =$TalenInfo->where(['user_id'=>$param['user_id'],'talent_id'=>$data['talent_id'],'type'=>4])->select();
            $resultes = [];
            //  $res = [];
            foreach ($data2 as $key => $value){
                // print_r($value['id']);
                $resultes[]=$value['content'];
            }
            $update ['self_introduction'] =$datas['content'];
            $update ['attachment_path'] =json_encode($resultes);
        }
        $takent->where(['talent_id'=>$data['talent_id']])->update($update);
        return $this->success("删除成功");
    }

    public  function  talentdata(Request $request){
        $param['user_id']=$this->userId;
        //print_r($param);
        $takent =new talentLogic();
        $data =$takent->where('user_id',$param['user_id'])->find();
        return $this->success(['data'=>$data]);
    }
}